﻿namespace Agent
{
    using Microsoft.Win32;
    using System;
    using System.Configuration;
    using System.Globalization;
    using System.IO;
    using System.Reflection;
    using System.Threading;
    using System.Windows;
    using TheSafety;

    internal class Program : Application
    {
        public static MainWindow a;
        public static Mutex b;

        private static bool a()
        {
            bool flag;
            b = new Mutex(false, "Prog_Mutex_OPEN_VPN_2011", out flag);
            return !flag;
        }

        [STAThread]
        private static void a(string[] A_0)
        {
            if (a())
            {
                ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                MessageBox.Show("You can not run a program twice", "Wizard Error", MessageBoxButton.OK, MessageBoxImage.Hand);
            }
            else
            {
                try
                {
                    ConfigManager.f(Assembly.GetExecutingAssembly().Location.Replace(Assembly.GetExecutingAssembly().ManifestModule.Name, ""));
                    ConfigManager.a(OSDetector.a());
                    ConfigManager.d(CultureInfo.CurrentUICulture.TwoLetterISOLanguageName);
                    a = new MainWindow();
                    new Application().Run(a);
                }
                catch (Exception exception)
                {
                    string str = string.Empty;
                    if (File.Exists(ConfigManager.q() + "/errorlog.txt"))
                    {
                        using (StreamReader reader = File.OpenText(ConfigManager.q() + "/errorlog.txt"))
                        {
                            str = reader.ReadToEnd();
                            reader.Close();
                        }
                    }
                    StreamWriter writer = File.CreateText(ConfigManager.q() + "/errorlog.txt");
                    if (string.IsNullOrEmpty(str))
                    {
                        writer.Write("=================++ ERROR LOG OF GUI ++=================");
                        writer.Write(Environment.NewLine);
                        writer.Write("====================-- BEGIN LOG --=====================");
                        writer.Write(Environment.NewLine);
                    }
                    else
                    {
                        writer.Write(str);
                    }
                    writer.Write(Environment.NewLine);
                    writer.Write(DateTime.Now.ToString() + " Source: " + exception.Source + " Message: " + exception.Message);
                    if (exception.InnerException != null)
                    {
                        writer.Write(Environment.NewLine);
                        writer.Write("Inner Source: " + exception.InnerException.Source + "Inner Message: " + exception.InnerException.Message);
                    }
                    writer.Flush();
                    writer.Close();
                    writer.Dispose();
                }
            }
        }

        private static void a(object A_0, PowerModeChangedEventArgs A_1)
        {
        }
    }
}

